#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

[ -f /etc/conf.d/gbox ] && . /etc/conf.d/gbox

PID=`pidof -o %PPID /usr/bin/cs2gbox.x86`
case "$1" in
  start)
    stat_busy "Starting cs2gbox"
    if [ -z "$PID" ]; then
      if [ "$CS2GBOX_IN_SCREEN" == "1" ]; then
        screen -X screen su - gbox -c /usr/bin/cs2gbox.x86 || screen -d -m su - gbox -c /usr/bin/cs2gbox.x86
      else
        su - gbox -c "/usr/bin/cs2gbox.x86 >> /var/log/cs2gbox 2>&1 &"
      fi
    fi
    if [ $? -gt 0 ]; then
      stat_fail
    else
      PID=`pidof -o %PPID /usr/bin/cs2gbox.x86`
      echo $PID >/var/run/cs2gbox.pid
      add_daemon cs2gbox
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping cs2gbox"
    [ ! -z "$PID" ] && kill $PID &>/dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon cs2gbox
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 5
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0
